<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--项目订单条件搜索-->
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="100px"
        >
          <el-form-item label="订单号" prop="orderNo">
            <el-input
              v-model="queryParams.orderNo"
              placeholder="请输入订单号"
              clearable
              style="width: 220px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="需求船舶类型" prop="shipType">
            <el-select
              v-model="queryParams.shipType"
              placeholder="请选择需求船舶类型"
              clearable
              style="width: 220px"
            >
              <el-option
                v-for="dict in shipDataList"
                :key="dict.info"
                :label="dict.info"
                :value="dict.info"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属项目名称" prop="projectName">
            <el-input
              v-model="queryParams.projectName"
              placeholder="请输入项目名称"
              clearable
              style="width: 220px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="联系人姓名" prop="contacts">
            <el-input
              v-model="queryParams.contacts"
              placeholder="请输入联系人姓名"
              clearable
              style="width: 220px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="联系人手机号" prop="contactPhone">
            <el-input
              v-model="queryParams.contactPhone"
              placeholder="请输入联系人手机号"
              clearable
              style="width: 220px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="归属企业" prop="companyId">
            <el-select
              v-model="queryParams.companyId"
              placeholder="请选择归属企业"
              clearable
              style="width: 220px"
            >
              <el-option
                v-for="dict in companyList"
                :key="dict.id"
                :label="dict.companyNickName"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="归属运营商" prop="operatorId">
            <el-select
              v-model="queryParams.operatorId"
              placeholder="请选择归属运营商"
              clearable
              style="width: 220px"
            >
              <el-option
                v-for="dict in operatorList"
                :key="dict.deptId"
                :label="dict.deptName"
                :value="dict.deptId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建方式" prop="createType">
            <el-select
              v-model="queryParams.createType"
              placeholder="请选择创建方式"
              clearable
              style="width: 220px"
            >
              <el-option label="运营" value="1" />
              <el-option label="用户" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建人姓名" prop="createName">
            <el-input
              v-model="queryParams.createName"
              placeholder="请输入创建人姓名"
              clearable
              style="width: 220px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建人手机号" prop="createName">
            <el-input
              v-model="queryParams.createPhone"
              placeholder="请输入创建人手机号"
              clearable
              style="width: 220px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 220px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
          <el-col :span="8">
            <el-tabs v-model="activeName" type="card" @tab-click="tabClick">
              <el-tab-pane label="全部" name=""></el-tab-pane>
              <el-tab-pane label="待审核" name="1"></el-tab-pane>
              <el-tab-pane label="进行中" name="2"></el-tab-pane>
              <el-tab-pane label="已暂停" name="3"></el-tab-pane>
              <el-tab-pane label="已结束" name="4"></el-tab-pane>
              <el-tab-pane label="已取消" name="5"></el-tab-pane>
            </el-tabs>
          </el-col>
          <el-col :span="9.2">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">
                发布项目订单
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="info"
                plain
                icon="el-icon-upload2"
                size="mini"
                @click="handleStop('')"
                :disabled="multiple"
              >
                批量暂停
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="multiple"
                @click="handleRestore('')"
              >
                批量恢复
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleOver('')"
              >
                批量结束
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
              >
                导出EXCEL
              </el-button>
            </el-col>
          </el-col>
        </el-row>

        <el-table
          border
          v-loading="loading"
          :data="projectList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="订单号" align="center" key="orderNo" prop="orderNo" :show-overflow-tooltip="true"/>
          <el-table-column
            label="需求船舶"
            align="center"
            key="shipNeededStr"
            prop="shipNeededStr"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="总数量"
            align="center"
            key="shipNeededTotalNum"
            prop="shipNeededTotalNum"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="订单有效期"
            align="center"
            key="deptName"
            prop="dept.deptName"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.startDate }} 至 {{ scope.row.endDate }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="所属项目名称"
            align="center"
            key="projectName"
            prop="projectName"
            width="120"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="联系人" align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.contacts }}({{ scope.row.contactPhone }})</span>
            </template>
          </el-table-column>
          <el-table-column
            label="归属企业"
            align="center"
            key="companyName"
            prop="companyName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="归属运营商"
            align="center"
            key="operatorName"
            prop="operatorName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="创建方式"
            align="center"
            key="createTypeStr"
            prop="createTypeStr"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.createTypeStr }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.createName }}({{ scope.row.createPhone }})</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" key="state">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.state == 1">{{
                scope.row.state | state
              }}</el-tag>
              <el-tag v-if="scope.row.state == 2">{{ scope.row.state | state }}</el-tag>
              <el-tag type="warning" v-if="scope.row.state == 3">{{
                scope.row.state | state
              }}</el-tag>
              <el-tag style="color: #dee2e6" color="rgba(0,0,0,0.4)" v-if="scope.row.state == 4">{{
                scope.row.state | state
              }}</el-tag>
              <el-tag type="info" v-if="scope.row.state == 5">{{ scope.row.state | state }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="300"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleDetail(scope.row)">
                查看详情
              </el-button>
              <el-button
                v-if="!['5', '4'].includes(scope.row.state)"
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
              >
                编辑
              </el-button>
              <el-button
                v-if="![5, 4, 3].includes(scope.row.state)"
                size="mini"
                type="text"
                style="color: #fd7900"
                @click="handleStop(scope.row.id)"
              >
                暂停
              </el-button>
              <el-button
                v-if="scope.row.state == 3"
                size="mini"
                type="text"
                style="color: #fd7900"
                @click="handleRestore(scope.row.id)"
              >
                恢复
              </el-button>
              <el-button
                v-if="![5, 4].includes(scope.row.state)"
                size="mini"
                type="text"
                style="color: red"
                @click="handleOver(scope.row.id)"
              >
                结束
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getOperateList, shipTypeEnum } from '@/api/common/index';
import { getOrderProjectList, setOrderState, getCompanySelectList } from '@/api/orders/orderProject';

export default {
  name: 'User',
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      projectList: [],
      // 日期范围
      dateRange: [],
      //项目类型选择
      activeName: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: '',
        shipType: '',
        projectName: '',
        contacts: '',
        contactPhone: '',
        companyId: '',
        operatorId: '',
        createType: '',
        createName: '',
        createPhone: '',
        createTimeStart: '',
        createTimeEnd: '',
      },
      companyList: [], //企业下啦列表
      operatorList: [], //运营商下拉列表
      shipDataList: [], //船舶类型下拉列表
    };
  },
  computed: {},
  watch: {},
  filters: {
    createType(val) {
      return val == 1 ? '运营' : '用户';
    },
    state(val) {
      return ['', '待审核', '进行中', '已暂停', '已结束', '已取消'][val];
    },
  },
  created() {
    this.getList();
    this.handleCompanyQuery();
    this.handleOperatorQuery();
    this.handleShipQuery();
  },
  methods: {
    // 获取船舶
    handleShipQuery() {
      shipTypeEnum().then((res) => {
        this.shipDataList = res.data;
      });
    },
    // 获取运营商
    handleOperatorQuery() {
      getOperateList().then((res) => {
        this.operatorList = res.data.operateList;
      });
    },
    //获取企业下啦列表
    handleCompanyQuery() {
      getCompanySelectList().then((res) => {
        this.companyList = res.data;
      });
    },
    //批量暂停订单
    async handleStop(id) {
      if (id) {
        this.ids.push(id);
      }
      this.$confirm(
        `${id ? '' : '已选' + this.ids.length + '个，'}确定将订单暂停？暂停后订单将不展示`,
        '暂停',
        {
          confirmButtonText: '暂停',
        },
      )
        .then(async () => {
          const data = await setOrderState({ ids: this.ids.join(','), state: '3' });
          if (data.code == 200) {
            this.$message.success(data.msg);
            this.ids = [];
            this.getList();
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(() => {});
    },
    //批量恢复订单
    async handleRestore(id) {
      if (id) {
        this.ids.push(id);
      }
      this.$confirm(
        `${id ? '' : '已选' + this.ids.length + '个，'}确定将订单恢复？恢复后订单可正常展示`,
        '恢复',
        {
          confirmButtonText: '恢复',
        },
      )
        .then(async () => {
          const data = await setOrderState({ ids: this.ids.join(','), state: '2' });
          if (data.code == 200) {
            this.$message.success(data.msg);
            this.ids = [];
            this.getList();
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(() => {});
    },
    //批量结束订单
    async handleOver(id) {
      if (id) {
        this.ids.push(id);
      }
      this.$confirm(
        `${id ? '' : '已选' + this.ids.length + '个，'}确定当前订单已结束？切换结束后无法恢复`,
        '结束',
        {
          confirmButtonText: '结束',
        },
      )
        .then(async () => {
          const data = await setOrderState({ ids: this.ids.join(','), state: '4' });
          if (data.code == 200) {
            this.$message.success(data.msg);
            this.ids = [];
            this.getList();
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(() => {});
    },
    /** 项目类型选择 */
    tabClick(tab) {
      this.queryParams.state = this.activeName;
      this.getList();
    },
    /**设置时间*/
    setArrTime() {
      if (this.dateRange.length == 2) {
        this.queryParams.createTimeStart = this.dateRange[0];
        this.queryParams.createTimeEnd = this.dateRange[1];
      } else {
        this.queryParams.createTimeStart = '';
        this.queryParams.createTimeEnd = '';
      }
    },
    /** 查询企业列表 */
    async getList() {
      this.setArrTime();
      this.loading = true;
      const data = await getOrderProjectList(this.queryParams);
      this.loading = false;
      if (data.code == 200) {
        this.projectList = data.data;
        this.total = data.total;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name: 'SaveOrderProject' });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ name: 'SaveOrderProject', query: { id: row.id } });
    },
    handleDetail(row) {
      this.$router.push({ name: 'OrderProjectDetail', query: { id: row.id } });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        '/orderProject/export',
        {
          ...this.queryParams,
        },
        `项目订单_${new Date().getTime()}.xlsx`,
      );
    },
  },
};
</script>
